package ms.jasim.console;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Panel;

import ms.spm.IAppContext;

import org.flexdock.docking.DockingConstants;
import org.flexdock.view.View;

import com.javadocking.dockable.DefaultDockable;
import com.javadocking.dockable.Dockable;
import com.javadocking.dockable.DockableState;
import com.javadocking.dockable.DraggableContent;
import com.javadocking.dockable.StateActionDockable;
import com.javadocking.dockable.action.DefaultDockableStateActionFactory;
import com.javadocking.drag.DragListener;

public class JasimDockablePanel extends Panel implements DraggableContent, JasimDockable {
	
	private static final long serialVersionUID = 1L;
	protected IAppContext context;
	protected JasimClientService svcJasim;
	private Object wrapper;
	private String text;
	private JasimDockManager dockManager;

	public JasimDockablePanel(IAppContext context) {
		super();
		this.context = context;
		setMinimumSize(new Dimension(80, 80));
		setupContext();
	}

	public String getText() {
		return text;
	}

	public void setText(String value) {
		text = value;
	}

	protected Dockable createSanaDockableView() {
		Dockable result = new DefaultDockable(this.getClass().getName(), this, getText(), null,
				(Integer) getDockProperty(DP_SANA_DOCKING_MODE));
		int[] states = { DockableState.NORMAL, DockableState.MINIMIZED, DockableState.MAXIMIZED, DockableState.EXTERNALIZED,
				DockableState.CLOSED };
		result = new StateActionDockable(result, new DefaultDockableStateActionFactory(), states);
		return result;
	}

	protected Component createFlexFockView() {
		View result = new View(this.getClass().getName(), getText());
		String[] actions = getDockProperty(DP_FLEXDOCK_ACTION);
		for (String act : actions)
			result.addAction(act);
		result.setContentPane(this);
		return result;
	}

	public <T> T getDockProperty(String propName) {
		if (dockManager instanceof FlexDockPanel)
			return getFlexDockProperty(propName);
		return null;
	}

	@SuppressWarnings("unchecked")
	protected <T> T getFlexDockProperty(String propName) {
		Object result = null;
		if (DP_DOCK_POSITION.equalsIgnoreCase(propName))
			result = DockingConstants.WEST_REGION;
		else if (DP_DOCK_SIZE.equalsIgnoreCase(propName))
			result = 0.3f;
		else if (DP_FLEXDOCK_ACTION.equalsIgnoreCase(propName))
			result = new String[] { DockingConstants.PIN_ACTION, DockingConstants.CLOSE_ACTION };
		return (T) result;
	}

	protected void setupContext() {
		svcJasim = context.getService(JasimClientService.class);
	}

	public void addDragListener(DragListener dragListener) {
		addMouseListener(dragListener);
		addMouseMotionListener(dragListener);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getDockableWrapper(JasimDockManager dockManager) {
		if (wrapper == null) {
			this.dockManager = dockManager;
			if (dockManager instanceof FlexDockPanel)
				wrapper = createFlexFockView();
			else
				wrapper = createSanaDockableView();
		}
		return (T) wrapper;
	}
}